package com.nf.health.app.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.WebViewActivity;
import com.nf.health.app.adapter.NewsAdapter;
import com.nf.health.app.customview.PullToRefreshView;
import com.nf.health.app.customview.PullToRefreshView.OnFooterRefreshListener;
import com.nf.health.app.customview.PullToRefreshView.OnHeaderRefreshListener;
import com.nf.health.app.http.Apis;
import com.nf.health.app.models.RegimenContent;
import com.nf.health.app.models.RegimenContentExtra;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.PreferenceHelper;
import com.nf.health.app.utils.ViewUtil;

import java.util.ArrayList;
import java.util.List;
@Deprecated
public class NewsFragment extends IBaseFragment {
	private final static String TAG = "NewsFragment";
	Activity activity;
	ListView mListView;
	NewsAdapter mAdapter;
	String text;
	String titleId;
	public final static int SET_NEWSLIST = 0;
	private static final String FLAG_REFRESH = "refresh";
	private static final String FLAG_MORE = "more";

	private int page = 1;
	private int page_size = 10;
	private List<RegimenContent> contentList = new ArrayList<RegimenContent>();
	private TextView emptyView;
	private PullToRefreshView pullToRefreshView;
	/**
	 * 
	 */
	private boolean isVisible;
	/**
	 * 标记初始化View是否完成
	 */
	private boolean isPrepare;
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		this.activity = activity;
		super.onAttach(activity);
	}
	/** 此方法意思为fragment是否可见 ,可见时候加载数据 */
	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		if (isVisibleToUser) {
			//fragment可见时加载数据
			isVisible = true;
			if(contentList !=null && contentList.size() !=0){
				if(mAdapter == null){
					mAdapter = new NewsAdapter(activity, contentList);
				}
				mListView.setAdapter(mAdapter);
			}else{
				initData(FLAG_REFRESH);
			}
		}else{
			//fragment不可见时不执行操作
			isVisible = false;
		}
		super.setUserVisibleHint(isVisibleToUser);
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		isPrepare = true;
		Bundle args = getArguments();
		text = args != null ? args.getString("text") : "";
		titleId = args != null ? args.getString("id") : "";
		
		addViewFillInRoot(R.layout.news_fragment);
		initView();
		initData(FLAG_REFRESH);
	}
	
	private void initData(String flag) {
		if(!isPrepare || !isVisible){
			//当其中一个不为true，则不加载数据；也就是仅当初始化完成，并且可见的时候才继续加载，这样的避免了未初始化完成就使用而带来的问题。
			//避免未初始化完成，可见状态就去调用initData方法，导致provider空指针问题
			return;
		}
		provider.requestRegimenContent(titleId,"",String.valueOf(page_size), String.valueOf(page),flag);		
	}
	private void initView() {
		mListView = ViewUtil.getView(rootView, R.id.mListView);
		pullToRefreshView = ViewUtil.getView(rootView, R.id.pull_refresh_view);
		pullToRefreshView.setShowRefreshTime(true);
		
		emptyView = new TextView(getActivity());
		emptyView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		emptyView.setText("暂无数据！");
		emptyView.setGravity(Gravity.CENTER);
		emptyView.setTextColor(getResources().getColor(R.color.black9));
		emptyView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
		emptyView.setVisibility(View.GONE);
		((ViewGroup)mListView.getParent()).addView(emptyView);
		
		pullToRefreshView.setOnHeaderRefreshListener(new OnHeaderRefreshListener() {//下拉刷新
			
			@Override
			public void onHeaderRefresh(PullToRefreshView view) {
				page = 1;
				initData(FLAG_REFRESH);
			}
		});
		
		pullToRefreshView.setOnFooterRefreshListener(new OnFooterRefreshListener() {//上拉加载
			
			@Override
			public void onFooterRefresh(PullToRefreshView view) {
				page++;
				initData(FLAG_MORE);
			}
		});
		
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if(contentList != null && contentList.size()>0){
					Bundle bundle = new Bundle();
					bundle.putString("httpUrl", Apis.WEB_LINK+"?articleid="+contentList.get(position).getDataid()+"&userid="+PreferenceHelper.getString(PreferenceHelper.USERID, ""));
					bundle.putString("docid",contentList.get(position).getDataid());//文章ID
					bundle.putString("title",contentList.get(position).getTitle());//文章tite
					bundle.putString("imgurl",contentList.get(position).getImg());//文章图片
					bundle.putString("content",contentList.get(position).getDescription());//文章副标题
					if (TextUtils.isEmpty(contentList.get(position).getMediaPath())
							|| (TextUtils.isEmpty(contentList.get(position).getMediaType()))) {
						bundle.putBoolean("colection",true);//文章是否可以收藏
					}else{
						bundle.putBoolean("colection", false);
						bundle.putString("mediaPath", contentList.get(position).getMediaPath());
						bundle.putString("mediaType", contentList.get(position).getMediaType());
					}
					bundle.putBoolean("isshare",true);
					ActivityUtils.showActivity(getActivity(), WebViewActivity.class, bundle);
				}
			}
		});
	}
	
	
	public void handleActionSuccess(String actionName, Object object) {
		if(FLAG_REFRESH.equals(actionName)){
			pullToRefreshView.onHeaderRefreshComplete();
			RegimenContentExtra extra = (RegimenContentExtra)object;
			List<RegimenContent> contents = extra.getList();
			if(contents!=null && contents.size()<0){
				mListView.setEmptyView(emptyView);
				return;
			}
			contentList.clear();
			contentList.addAll(contents);
		}else if(FLAG_MORE.equals(actionName)){
			pullToRefreshView.onFooterRefreshComplete();
			RegimenContentExtra extra = (RegimenContentExtra)object;
			List<RegimenContent> contents = extra.getList();
			contentList.addAll(contents);
		}
		if(mAdapter == null){
			mAdapter = new NewsAdapter(activity, contentList);
		}
		
		mListView.setAdapter(mAdapter);
		mListView.setOnScrollListener(mAdapter);
//		mListView.setPinnedHeaderView(LayoutInflater.from(activity).inflate(R.layout.list_item_section, mListView, false));
	}

	/* 摧毁视图 */
	@Override
	public void onDestroyView() {
		super.onDestroyView();
		Log.d("onDestroyView", "channel_id = " + titleId);
		mAdapter = null;
	}
	/* 摧毁该Fragment，一般是FragmentActivity 被摧毁的时候伴随着摧毁 */
	@Override
	public void onDestroy() {
 		super.onDestroy();
		Log.d(TAG, "channel_id = " + titleId);
	}
}
